/* ========================================================================
 * jQuery: loaderer.js v0.0.1_Alpha 
 * url
 * ========================================================================
 */

+ function ($) {
    var LOADER = function (elem, options) {
        this.options = options;
        this.self = $(elem);
    };
    LOADER.VERSION = 'v0.0.1_Alpha ';
    LOADER.DEFAULTS = {
        size: 50,
        text: "loading...", //载入提示文字
        textColor: '',
        layout: "", //line（暂时只有line样式）
        toast: false,//toast模式下是有遮罩层的
        $animate: '',
        $loadEl: '',
        $text: '',
        custom: '' //自定义动画
    }
    LOADER.prototype.init = function () {
        var self = this.self;
        var html = this.options.custom;
        var $mask = $('<div></div>', {
            id: 'loader-mask'
        });
        var $loader = $('<div></div>', {
            class: 'nv-loader'
        });
        this.$text = $('<p></p>', {
            html: this.options.text
        });
        if (this.options.textColor) {
            this.$text.css({
                "color": this.options.textColor
            });
        }
        this.$animate = $(this.createAnimate(html));
        $loader.append(this.$animate).append(this.$text).addClass(this.options.layout ? 'nv-loader-line' : '');
        this.$loadEl = $loader;
        if (this.options.toast) {
            $loader.addClass("nv-loader-toast");
            this.$loadEl = $mask.append($loader);
        }
    }
    LOADER.prototype.createAnimate = function (html) {
        if (!html) {
            switch (this.options.style) {
                case "line":
                    html = '<div class="nv-loader-line-scale"><div></div><div></div><div></div><div></div><div></div></div>';
                    break;
                default:
                    html = '<div class="nv-loader-loop-box"><div class="nv-loader-loop-left"><div class="nv-loader-loop"></div></div><div class="nv-loader-loop-right"><div class="nv-loader-loop"></div></div></div>';
                    break;
            }
        }
        return html
    }
    LOADER.prototype.open = function (option) {
        if (typeof option == 'string') {
            this.$animate.remove(); //移除动画
            this.$text.text(option);
        }
        $(this.self).append(this.$loadEl);
    }
    //完全移除,无法通过open再次调出
    LOADER.prototype.destroy = function () {
        this.$loadEl.remove();
    }
    //暂时关闭
    LOADER.prototype.close = function (mode) {
        if (mode) {
            this.$loadEl.addClass("nv-loader-hidden");
        } else {
            this.$loadEl.addClass("nv-loader-close");
        }
    }

    function Plugin(option) {
        var $this = $(this);
        var data = $this.data('jq.loader');
        var options = $.extend({}, LOADER.DEFAULTS, $this.data(), typeof option == 'object' && option)
        if (!data) {
            $this.data('jq.loader', (data = new LOADER(this, options)));
            data.init();
            return data;
        }
    }
    $.fn.loader = Plugin;
}(jQuery);